package logic

import (
	"context"
	"fmt"
	"novel/model/mysql"
	"strings"

	"novel/novelrpc/internal/svc"
	"novel/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CommentAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommentAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommentAddLogic {
	return &CommentAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 小说评价
func (l *CommentAddLogic) CommentAdd(in *novelrpc.CommentAddRequest) (*novelrpc.CommentAddResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	mysql.DB.Where("id=?", in.UserId).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("该用户不存在")
	}

	if strings.Contains(in.ConText, "抄袭") || strings.Contains(in.ConText, "涉政") {
		return nil, fmt.Errorf("出现敏感词已拦截")
	}

	data := mysql.Comment{
		UserId:  in.UserId,
		BookId:  in.BookId,
		ConText: in.ConText,
	}
	mysql.DB.Create(&data)
	return &novelrpc.CommentAddResponse{
		CommentId: int64(data.ID),
	}, nil
}
